//
//  XJCmtIdResultView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/13.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  社区认证结果view(审核中、已认证)

#import "XJCmtIdResultView.h"
#import "XJCmtIdResultViewHeaderView.h" //区头
#import "XJCmtIdResultViewCell.h" //cell
@interface XJCmtIdResultViewModel : NSObject
@property (nonatomic, copy) NSString *leftStr;
@property (nonatomic, copy) NSString *rightStr;
@end
@implementation XJCmtIdResultViewModel
@end


@interface XJCmtIdResultView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XJCmtIdResultViewHeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation XJCmtIdResultView

#pragma mark -  一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.estimatedRowHeight = 59;// 44 +15
        
        NSString *clsName = NSStringFromClass([XJCmtIdResultViewCell class]);
        [_tableView registerNib:[UINib nibWithNibName:clsName bundle:nil] forCellReuseIdentifier:clsName];
        
        _tableView.tableFooterView = [UIView new];
        
        _tableView.scrollEnabled = NO; //不让滚动
    }
    return _tableView;
}

- (XJCmtIdResultViewHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[XJCmtIdResultViewHeaderView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 250)];
    }
    return _headerView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark -  二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self stupView];
    }
    return self;
}
- (void)stupView {
    self.tableView.frame = self.bounds;
    [self addSubview:self.tableView];
    
    self.tableView.tableHeaderView = self.headerView;
}


#pragma mark -  三 UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 59; //44+ 15
}
#pragma mark -  三 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *clsName = NSStringFromClass([XJCmtIdResultViewCell class]);

    XJCmtIdResultViewCell *cell = [tableView dequeueReusableCellWithIdentifier:clsName];
    
    XJCmtIdResultViewModel *model = self.dataArray[indexPath.row];
    [cell configureXJCmtIdResultViewCellWithLeftStr:model.leftStr rightStr:model.rightStr];
    
    return cell;
}

#pragma mark - 四 赋值
- (void)configureXJCmtIdResultViewWithDetailDic:(NSDictionary *)dic status:(NSNumber *)state {

    if ([state isEqualToNumber:@1]) {
        [self.headerView configureXJCmtIdResultViewHeaderViewWithStateLb:@"社区认证审核中"];
    }else{
        [self.headerView configureXJCmtIdResultViewHeaderViewWithStateLb:@"社区已认证"];
    }

    
    self.dataArray = [self modelArrayConvertFromDic:dic];
    
    [self.tableView reloadData];
}


#pragma mark - 五 Private method
//将服务返回的dic转换为model数组
- (NSMutableArray *)modelArrayConvertFromDic:(NSDictionary *)dic {
    
    NSString *const leftStr = @"leftStr";
    NSString *const rightStr = @"rightStr";
    NSString *roomName = dic[@"room_name"]; //房间名称
    NSString *name = dic[@"proprietor_name"];//业主姓名
    NSString *phone = dic[@"proprietor_phone"];//业主手机号
    NSString *relation = dic[@"proprietor_relation"]; //与业主关系
    
    NSDictionary *modelDic1 = @{leftStr:@"房间名称",
                                rightStr:roomName
                                };
    NSDictionary *modelDic2 = @{leftStr:@"业主姓名",
                                rightStr:name
                                };
    NSDictionary *modelDic3 = @{leftStr:@"业主手机号",
                                rightStr:phone
                                };
    NSDictionary *modelDic4 = @{leftStr:@"与业主关系",
                                rightStr:relation
                                };

    NSArray *dicArr = @[modelDic1,modelDic2,modelDic3,modelDic4];
    
   //转model
    NSMutableArray *newArr = [NSMutableArray array];
    for (NSDictionary *tempDic in dicArr) {
        XJCmtIdResultViewModel *model = [XJCmtIdResultViewModel mj_objectWithKeyValues:tempDic];
        [newArr addObject:model];
    }
    
    return newArr;
}

/**
 {
 "data" : {
 "detail" : {
 "status" : 1,
 "id" : 36,
 "room_name" : "123",
 "proprietor_name" : "Dad ",
 "create_time" : 1531709383980,
 "proprietor_phone" : "15837694444",
 "customer_id" : 7,
 "proprietor_relation" : "Drag ",
 "community_id" : 9
 }
 },
 "code" : 0
 }

 */

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
